package com.ilvyou.api.service;

import com.ilvyou.api.bean.param.AddBank;
import com.ilvyou.core.base.BaseService;
import com.ilvyou.core.dao.Criteria;
import com.ilvyou.core.dao.Dao;
import com.ilvyou.core.util.StringUtil;
import com.ilvyou.core.util.Util;
import com.ilvyou.data.lhf.AlipayBanktypeEntity;
import com.ilvyou.data.lhf.AlipayPbankEntity;
import com.ilvyou.data.lhf.AlipayQpcardEntity;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;

/**
 * Created by GuanYuCai on 2016/9/8 0008.
 */
@Service
public class BankService extends BaseService {

    @Resource(name = "lhfDao")
    private Dao lhfDao;

    @Override
    protected Dao getDao() {
        return lhfDao;
    }

    public void deleteBankCard(AlipayAccountEntity account, String card) throws Exception{
        AlipayPbankEntity bank = lhfDao.getByPK(AlipayPbankEntity.class, card);
        lhfDao.deleteByPK(AlipayPbankEntity.class, card);

        // 删除对应的快捷支付卡
        lhfDao.delete(AlipayQpcardEntity.class, new Criteria("banknumber=?", card));

        account.setCardqty(account.getCardqty() - 1);
        if(("Debit").equals(bank.getBanktype())){
            if(bank.getAccounttype() == 1){
                account.setPcardqty((account.getPcardqty() - 1) < 0 ? 0 : (account.getPcardqty() - 1));
            }else{
                account.setCcardqty((account.getCcardqty() - 1) < 0 ? 0 : (account.getCcardqty() - 1));
            }
        }else if(("Credit").equals(bank.getBanktype())){
            account.setPcardqty((account.getPcardqty() - 1) < 0 ? 0 : (account.getPcardqty() - 1));
        }
        account.setLastedon(new Timestamp(System.currentTimeMillis()));
        lhfDao.update(account);
    }

    public void addBankCard(AddBank params, AlipayPbankEntity pbank, AlipayBanktypeEntity bankType) throws Exception{
        //根据actId查询用户
        AlipayAccountEntity account = getByPK(AlipayAccountEntity.class, params.getActid());
        if(pbank == null){
            //添加aipay_pbank 相对应数据  数据库插入数据
            AlipayPbankEntity pbankEntity = creatPbank(params, account, bankType);
            lhfDao.insert(pbankEntity);

            //添加alipay_qpcard 相对应数据  数据库插入数据
            lhfDao.insert(creatQpcard(Util.createId(), pbankEntity));

            //银行卡数量 +1
            account.setCardqty(account.getCardqty() + 1);
            if(1 == params.getAccounttype()){ //个人账户
                account.setPcardqty(account.getPcardqty() + 1);     //个人银行卡 + 1
            }else if(2 == params.getAccounttype()){ //对公账户
                account.setCcardqty(account.getCcardqty() + 1);     //对公银行卡 + 1
            }

            //更新表aipay_account 银行卡数量 CardQty PCardQty或者CCardQty
            account.setLastedon(new Timestamp(System.currentTimeMillis()));
            lhfDao.update(account);
        }else if(pbank.getRechargeflag() == 0){
            //银行卡中是否充值字段为0,即不可充值，则向alipay_qpcard快捷支付卡中添加一条记录，并且修改是否充值字段为1，即可充值
            //添加alipay_qpcard 相对应数据  数据库插入数据
            lhfDao.insert(creatQpcard(Util.createId(), pbank));

            //置alipayPbank中的是否充值字段rechargeflag为1
            pbank.setRechargeflag(1);
            pbank.setLastedby(account.getActid());
            pbank.setLastedon(new Timestamp(System.currentTimeMillis()));
            pbank.setLastedname(account.getActname());
            lhfDao.update(pbank);
        }
    }

    private AlipayQpcardEntity creatQpcard(long pkid, AlipayPbankEntity pbank) throws Exception {
        AlipayQpcardEntity c = new AlipayQpcardEntity();
        Util.transfer(pbank, c, "actid,actname,realname,bankid,bankcode,bankname,banknumber,banktype," +
                "duemonth,dueyear,authcode,companyid,companycode,companyname,companyfname,companyaccount,companymobile");
        c.setPkid(pkid);
        c.setCredcode(pbank.getPidcard());
        c.setBuyertel(pbank.getPmobile());
        c.setBuyername(pbank.getPname());
        c.setEnableflag((short) 1);
        c.setDeleteflag((short) 0);
        c.setCreatedname(pbank.getActname());
        c.setCreatedby(pbank.getActid());
        c.setLastedname(pbank.getActname());
        c.setLastedby(pbank.getActid());
        return c;
    }

    private AlipayPbankEntity creatPbank(AddBank addBank, AlipayAccountEntity account, AlipayBanktypeEntity bankType) throws Exception {
        AlipayPbankEntity bank = new AlipayPbankEntity();

        if(!StringUtil.isEmpty(addBank.getDueyear())){
            String dueyear = addBank.getDueyear();
            if(dueyear.length() > 2){
                dueyear = dueyear.substring(2);
            }
            bank.setDueyear(dueyear);
        }

        Util.transfer(account, bank, "actid,actname,realname");
        bank.setBankcode(bankType.getBankcode());
        bank.setBankname(bankType.getBankname());
        bank.setBankid(addBank.getBankid());
        bank.setBanknumber(addBank.getCardnumber());
        bank.setBanktype(addBank.getBanktype());
        bank.setDuemonth(addBank.getDuemonth());
        bank.setAuthcode(addBank.getCardverifycode());
        bank.setAccounttype(addBank.getAccounttype());
        bank.setPname(addBank.getHoldername());
        bank.setPidcard(addBank.getHolderid());
        bank.setBankbranch(addBank.getOpenbankname());

        if("1".equals(addBank.getAccounttype())){
            bank.setPmobile(addBank.getHolderphone());
        }else{
            bank.setCompanymobile(addBank.getHolderphone());
            bank.setCompanyaccount(addBank.getCompanyaccount());
        }

        bank.setDefaultflag((short) 0);
        bank.setEnableflag((short) 1);
        bank.setLockflag((short) 0);
        bank.setCreatedname(account.getActname());
        bank.setCreatedby(account.getActid());
        bank.setLastedname(account.getActname());
        bank.setLastedby(account.getActid());
        bank.setRechargeflag(1);

        return bank;
    }
}
